package com.hitrust.gs.business;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.util.DefineDic;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureUploadVO;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.dao.GSProviderDAO;

public class GSTempManufactureManager {

	private static String AREA_SH = "SH";
	private static String AREA_SZ = "SZ";
	
	private static String CURRENT_AREA = AREA_SZ;
	private GSManufactureDAO dao = new GSManufactureDAO();
	public static void main(String args[]){
		GSTempManufactureManager m = new GSTempManufactureManager ();
		
		try{
			
			//m.batchImportManufacture("F:/TEMP_BUS/18_01.xls","user");
			//System.out.println("F:/TEMP_BUS/18_01.xls end...");
			//m.batchImportManufacture("F:/TEMP_BUS/18_02.xls","user");
			//System.out.println("F:/TEMP_BUS/18_02.xls end...");
			//m.batchImportManufacture("F:/TEMP_BUS/18_03.xls","user");
			//System.out.println("F:/TEMP_BUS/18_03.xls end...");
			//m.batchImportManufacture("F:/TEMP_BUS/18_04.xls","user");
			//System.out.println("F:/TEMP_BUS/18_04.xls end...");
			//m.batchImportManufacture("F:/TEMP_BUS/18_05.xls","user");
			//System.out.println("F:/TEMP_BUS/18_05.xls end...");
			
			//m.batchImportManufacture("F:/TEMP_BUS/18_11.xls","user");
			//System.out.println("F:/TEMP_BUS/18_11.xls end...");
			//m.batchImportManufacture("F:/TEMP_BUS/18_12.xls","user");
			//System.out.println("F:/TEMP_BUS/18_12.xls end...");
			
			m.batchImportManufacture("e:/18_22.xls","user");
			System.out.println("F:/TEMP_BUS/18_21.xls end...");
			
			//m.batchImportManufacture("F:/TEMP_BUS/18_31.xls","user");
			//System.out.println("F:/TEMP_BUS/18_31.xls end...");
			//m.batchImportManufacture("F:/TEMP_BUS/18_32.xls","user");
			//System.out.println("F:/TEMP_BUS/18_32.xls end...");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	public String batchImportManufacture(String aFile, String aUserName)
			throws Exception {

		int tSuccCount = 0;
		int tFailCount = 0;
		String tFailFile = "0";

		HSSFWorkbook tHSSFWorkbook = new HSSFWorkbook();
		HSSFSheet tHSSFSheet = null;
		try {
			// 开始读取excel文件
			tHSSFWorkbook = new HSSFWorkbook(new DataInputStream(new BufferedInputStream(
					new FileInputStream(new File(aFile))))); // 读取sheet表单
			// 读取sheet表单
			tHSSFSheet = tHSSFWorkbook.getSheetAt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ArrayList tList = new ArrayList();
		// 读取并检查明细
		int tRowCount = getManufactureContents(tList, tHSSFSheet);
		System.out.println("有效数据共："+tRowCount);
		ArrayList aryl = new ArrayList();
		Connection tConn = null;
		try {
			
			tConn = getConn();//tGSManufactureDAO.getConnetion();
			tConn.setAutoCommit(false);
			
			HashMap map = this.getAllProvider(tConn);
			if (tList.size() > 0) {
				int errorNum = 0;
				for (int i = 0; i < tList.size(); i++) {
					System.out.println("i:"+i);
					GSManufactureBeanTemp temp = (GSManufactureBeanTemp)tList.get(i);
				
					if(map.containsKey(temp.getGs_provider_id())){
						String providerId = (String)map.get(temp.getGs_provider_id());
						temp.setGs_provider_id(providerId.toString());	
					}else{
						//System.out.println("id为："+temp.getGs_manufacture_id()+"的产品供应商<"+temp.getGs_provider_id()+">未找到");
						aryl.add(temp.getGs_manufacture_id()+"<"+temp.getGs_provider_id()+">");
						errorNum ++;
						continue;
					}
					
					//更新产品表
					this.updateManufacture(dao,tConn,temp);
					//更新订单明细表
					//this.updateOrderDetail(dao,tConn,temp);
					//更新应急采购过渡表
					//this.updateOrderTempDetail(dao,tConn,temp);
					
				}
				System.out.println("出错供应商数据共:"+errorNum);
			}
			tConn.commit();
			//tConn.rollback();
		} catch (Exception e) {
			e.printStackTrace();
			tConn.rollback();
		} finally{
			tConn.close();
		}

		System.out.println("\n************************************");
		
		for(int i=0;i<aryl.size();i++){
			System.out.println(aryl.get(i));
		}
		return tSuccCount + "-" + tFailCount + "-" + tFailFile;
	}

	private static int getProviderContents(ArrayList aList,	HSSFSheet aHSSFSheet) throws Exception {
		int i = 0;
		// 取得第一行的对象
		HSSFRow tFirstRow = aHSSFSheet.getRow(0);
		

		while (true ) {
			HSSFRow tHSSFRow = aHSSFSheet.getRow(1 + i);
			boolean flag = false;
			try {
					if (tHSSFRow == null) {
						break;
					}
					if (tHSSFRow.getCell((short) 2) == null || tHSSFRow.getCell((short) 0) == null) {
						break;
					}
                    	
                	HSSFCell cell_old = tHSSFRow.getCell((short)0);
                	HSSFCell cell_new = tHSSFRow.getCell((short)2);
                	
                	String oldv = "";String newv = "";
                	if(HSSFCell.CELL_TYPE_NUMERIC ==cell_old.getCellType()){
                		oldv = String.valueOf((int)(cell_old.getNumericCellValue())).trim();
                	}else{
                		oldv = String.valueOf(cell_old.getStringCellValue()).trim();
                	}
                	if(HSSFCell.CELL_TYPE_NUMERIC ==cell_new.getCellType()){
                		newv = String.valueOf((int)(cell_new.getNumericCellValue())).trim();
                	}else{
                		newv = String.valueOf(cell_new.getStringCellValue()).trim();
                	}
                    String ids[] = {oldv,newv};
                    aList.add(ids);
                    i++;

			} catch (RuntimeException e) {
				throw new Exception("导入数据中"+(i+1)+"列数据类型不符合规定.");
			} catch (Error e) {
				throw new Exception(e.getMessage());
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return i;
	}	

	/**
	 * 读取excel产品信息文档内容
	 * 
	 * @param aList
	 *            ArrayList对象，存放读到的内容
	 * @param aSheet
	 *            表单
	 * @throws CUPSException
	 */
	private static int getManufactureContents(ArrayList aList,
			HSSFSheet aHSSFSheet) throws Exception {
		int i = 0;
		// 取得第一行的对象
		HSSFRow tFirstRow = aHSSFSheet.getRow(0);
		
		int xc = 0;
		while (true ) {
			HSSFRow tHSSFRow = aHSSFSheet.getRow(1 + i);
			boolean flag = false;
			try {
					if (tHSSFRow == null) {
						break;
					}
					if (tHSSFRow.getCell((short) 2) == null || tHSSFRow.getCell((short) 3) == null) {
						break;
					}
					HSSFCell tempCell = null;
					//根据地区字段过滤产品
					//tempCell = tHSSFRow.getCell((short)34);
					//if(!CURRENT_AREA.equals(String.valueOf(tempCell.getStringCellValue()).trim())){
						//System.out.println("continue..."+i);
						//i++;xc++;
						//continue;
					//}
					//生产厂商是N/A的属小车平台，无须处理
					tempCell = tHSSFRow.getCell((short)11);
					if("N/A".equals(tempCell.getStringCellValue().trim())){
						//System.out.println("continue..."+i);
						i++;xc++;
						continue;
					}
					
					//过滤重复数据
					tempCell = tHSSFRow.getCell((short)14);
					double dkhj = 0;
					//如果本行的大客户价为0
					if(tempCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
						dkhj = tempCell.getNumericCellValue();						
					}else{
						dkhj = Double.parseDouble(tempCell.getStringCellValue().trim());
					}
					
					if(0==dkhj){
						String newid = tHSSFRow.getCell((short)2).getStringCellValue();
						String lastnewid = aHSSFSheet.getRow(i).getCell((short)2).getStringCellValue();
						//如果本行新id和上一行的新id相等，则本行
						if(newid.equals(lastnewid)){
							i++;xc++;
							continue;
						}
					}
                    	

                    GSManufactureUploadVO tGSManufactureUploadVO=new GSManufactureUploadVO();
                    int tResult = 1;
                    String tFailReason = "";
                    
                    GSManufactureBeanTemp tGSManufactureBean = new GSManufactureBeanTemp();
                    boolean sign = false;
                    for(int j=0; j<=34; j++){
                    	//System.out.print(j==34?"\n"+j:""+j);
                        //String tFiledName = tFirstRow.getCell((short)j).getStringCellValue();
                    	HSSFCell cell = tHSSFRow.getCell((short)j);
                         //产品编号
                        if(j==3){
                        	if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                                String tManufactureId= String.valueOf(new Double(cell.getNumericCellValue())).trim();
                                tGSManufactureBean.setGs_manufacture_id(tManufactureId);
                        		
                        	}else{
                                String tManufactureId= String.valueOf(cell.getStringCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_id(tManufactureId);
                        	}
                         //产品新编号   
                        }else if(j==2){
                        	if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tCusItemCode = String.valueOf(cell.getNumericCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_id_temp(tCusItemCode);
                        		
                        	}else{
                        		String tCusItemCode = String.valueOf(cell.getStringCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_id_temp(tCusItemCode);
                        	}
                         //产品名称
                        }else if(j==5){
                            String tManufactureName = String.valueOf(cell.getStringCellValue());
                            tGSManufactureBean.setGs_manufacture_name(tManufactureName==null?"":tManufactureName.trim());
                         //产品种类
                        }else if(j==1){
                        	if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                                String tCategoryCode = String.valueOf((int)cell.getNumericCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_category_id(tCategoryCode);
                        		
                        	}else {
                                String tCategoryCode = String.valueOf(cell.getStringCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_category_id(tCategoryCode);
                        		
                        	}
                        //产品品牌  
                        }else if(j==10){
                        	if(cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tProductBrand = String.valueOf(cell==null?"":""+cell.getNumericCellValue());
                        		tGSManufactureBean.setGs_manufacture_brand(tProductBrand==null?"":tProductBrand.trim());	
                        	}else{
                        		String tProductBrand = cell==null?"":cell.getStringCellValue();
                        		tGSManufactureBean.setGs_manufacture_brand(tProductBrand==null?"":tProductBrand.trim());
                        	}
                         //产品型号   
                        }else if(j==7){
                        	if(cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tProductType = String.valueOf(cell==null?"":""+cell.getNumericCellValue());
                        		tGSManufactureBean.setGs_manufacture_type(tProductType==null?"":tProductType.trim());	
                        	}else{
                        		String tProductType = cell==null?"":cell.getStringCellValue();
                        		tGSManufactureBean.setGs_manufacture_type(tProductType==null?"":tProductType.trim());
                        	}
                         //产品规格  
                        }else if(j==6){
                        	if(cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tProductSpec = String.valueOf(cell==null?"":""+cell.getNumericCellValue());
                        		tGSManufactureBean.setGs_manufacture_spec(tProductSpec==null?"":tProductSpec.trim());	
                        	}else{
                        		String tProductSpec = cell==null?"":cell.getStringCellValue();
                        		tGSManufactureBean.setGs_manufacture_spec(tProductSpec==null?"":tProductSpec.trim());
                        	}
                        	
                         //产品单价
                         //产品单价本次无须修改，在执行数据库modify的时候不执行单价的修正
                        }else if(j==14){
                            if(cell != null && !cell.toString().equals("") && !cell.toString().equals("0")){
                                String tProductPrice = String.valueOf(cell.getNumericCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_unitprice(Double.parseDouble(tProductPrice));
                            }else{
                            	HSSFCell simpleCell = tHSSFRow.getCell((short)13);
                            	String tProductPrice = String.valueOf(cell.getNumericCellValue()).trim();
                                tGSManufactureBean.setGs_manufacture_unitprice(Double.parseDouble(tProductPrice));
                            }
                        //数量单位  
                        }else if(j==12){
                            String tProductUnit = cell.getStringCellValue().trim();
                            tGSManufactureBean.setGs_quant_unit(tProductUnit);
                        //质保期   
                        }else if(j==15){
                          
                        	if(cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tAssurePeriod = String.valueOf(cell==null?"":""+cell.getNumericCellValue());
                        		tGSManufactureBean.setGs_quality_assure_period(tAssurePeriod==null?"":tAssurePeriod.trim());	
                        	}else{
                        		String tAssurePeriod = cell==null?"":cell.getStringCellValue();
                        		tGSManufactureBean.setGs_quality_assure_period(tAssurePeriod==null?"":tAssurePeriod.trim());
                        	}
                        //生产厂商名称    
                        }else if(j==11){
                            String tManufacturer = cell.getStringCellValue().trim();
                            tGSManufactureBean.setGs_manufacturer_name(tManufacturer);
                        //产地   
                        }else if(j==9){
                            String tMakeArea = cell==null?"":String.valueOf(cell.getStringCellValue()).trim();
                            tGSManufactureBean.setGs_make_area(tMakeArea);
                        //供应商编号,此处的供应商编号为供应商全称，需要另外转换供应商id
                        }else if(j==17){
                        	if(i==1140)
                        		System.out.print("");
                        	String buffer = String.valueOf(cell.getStringCellValue());
                        	if(buffer.indexOf("(仓储中心配送)") != -1 || buffer.indexOf("(仓储中心)") != -1){
                        		sign = true;
                        		buffer = buffer.replaceAll("仓储中心配送"," ");
                        		buffer = buffer.replaceAll("仓储中心"," ");
                        		buffer = buffer.replace('(',' ');
                        		buffer = buffer.replace(')',' ');
                        	}
                            String tCompanyName = buffer.trim();
                            tGSManufactureBean.setGs_provider_id(tCompanyName);
                        //适用机型    
                        }else if(j==21){                           
                        	if(cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tMachineType = String.valueOf(cell==null?"":""+cell.getNumericCellValue());
                        		tGSManufactureBean.setGs_adapt_machine_type(tMachineType==null?"":tMachineType.trim());	
                        	}else{
                        		String tMachineType = cell==null?"":cell.getStringCellValue();
                        		tGSManufactureBean.setGs_adapt_machine_type(tMachineType==null?"":tMachineType.trim());
                        	}
                        //产品图片  
                        }else if(j==18){
                            String tProductImage = cell==null?"":cell.getStringCellValue().trim();
                            if(tProductImage.equals(null)){
                                tGSManufactureBean.setGs_manufacture_images("");    
                            }else{
                                tGSManufactureBean.setGs_manufacture_images(tProductImage);
                            }
                        //产品附加属性
                        }else if(j==33){
                            String tExProperties = cell==null?"":cell.getStringCellValue().trim();
                            tGSManufactureBean.setGs_appender_property(tExProperties);
                        }
                        //备注
                        else if(j==16){
                        	if(cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                        		String tRemark = String.valueOf(cell==null?"":""+cell.getNumericCellValue());
                        		tGSManufactureBean.setGs_remark(tRemark==null?"":tRemark.trim());	
                        	}else{
                        		String tRemark = cell==null?"":cell.getStringCellValue();
                        		tGSManufactureBean.setGs_remark(tRemark==null?"":tRemark.trim());
                        	}
                            
                            
                        }
                        //是否有效
                        else if(j==0){
                            if(cell != null && "退会".equals(cell.getStringCellValue()))
                            	tGSManufactureBean.setGs_validity("0");
                            else 
                            	tGSManufactureBean.setGs_validity("1");
                        }
                    }
                    
                    tGSManufactureBean.setGs_recent_adjusted_price(Double.parseDouble("0"));
                    tGSManufactureBean.setGs_recent_adjustprice_date("");
                    if(sign){
                    	tGSManufactureBean.setGs_is_tradeflat_manufacture("0");//根据供应商名称的备注决定是否特约产品                   	
                    }else{
                    	tGSManufactureBean.setGs_is_tradeflat_manufacture("1");//默认网上产品
                    }
                    tGSManufactureBean.setGs_concentricity(tGSManufactureBean.getGs_manufacture_id_temp().substring(0,14));

                    aList.add(tGSManufactureBean);
                    i++;


			} catch (RuntimeException e) {
				throw new Exception("导入数据中"+(i+1)+"列数据类型不符合规定.");
			} catch (Error e) {
				throw new Exception(e.getMessage());
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		System.out.println("XC:"+xc);
		return i;
	}	

	private void updateOrderDetail(GSManufactureDAO dao,Connection conn,GSManufactureBeanTemp temp) throws Exception {
		String sql = "update GS_ORDER_DETAIL SET gs_manufacture_id = ?,gs_concentricity_id = ? where gs_manufacture_id = ?";
		String[] ids = {temp.getGs_manufacture_id_temp(),temp.getGs_concentricity(),temp.getGs_manufacture_id()};
		boolean bool = dao.executeUpdate(conn,sql,ids);
		if(!bool){
			//System.out.println("原id："+ids[1]+",现id"+ids[0]+"在更新订单明细表时执行失败");
		}
	}
	private void updateOrderTempDetail(GSManufactureDAO dao,Connection conn,GSManufactureBeanTemp temp) throws Exception {
		String sql = "update GS_ORDER_TEMP_DETAIL SET gs_manufacture_id = ?,gs_concentricity_id = ? where gs_manufacture_id = ?";
		String[] ids = {temp.getGs_manufacture_id_temp(),temp.getGs_concentricity(),temp.getGs_manufacture_id()};
		boolean bool = dao.executeUpdate(conn,sql,ids);
		if(!bool){
			//System.out.println("原id："+ids[1]+",现id"+ids[0]+"在更新应急采购表时执行失败");
		}
	}
	private void updateManufacture(GSManufactureDAO dao,Connection conn,GSManufactureBeanTemp temp) throws Exception {
		boolean bool = false;
		if(isExist(dao,conn,temp.getGs_manufacture_id())){
			bool = this.modify(conn,temp);
		}else{
			bool = this.add(conn,temp);
		}
		
		if(!bool){
			System.out.println("原id："+temp.getGs_manufacture_id()+",现id"+temp.getGs_manufacture_id_temp()+"在更新产品表时执行失败");
		}
	}

	
	private Connection getConn(){
		Connection conn = null;
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");			
			conn = DriverManager.getConnection("jdbc:oracle:thin:@61.152.242.71:1521:mian","gs2","gs2");
		}catch(Exception e){
			e.printStackTrace();			
		}
		return conn;
	}
	
    public boolean isExist(GSManufactureDAO dao,Connection conn,String aManfactureId) throws GSException{
        String tSql = "select * from GS_Manufacture where GS_MANUFACTURE_ID = ?";
        boolean tFlag = false;
        Object[] tObj = {aManfactureId};
        try{
            ArrayList tList = dao.executeQuery(conn,new GSManufactureBean(),tSql,tObj);
            if(tList.size() > 0){
                tFlag = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tFlag;
    }
    
    public HashMap getAllProvider(Connection conn){
    	String tSql = "select * from gs_provider ";
    	HashMap map = new HashMap();
        try{
            ArrayList tList = dao.executeQuery(conn,new GSProviderBean(),tSql, null);
            
            for(int i=0;i<tList.size();i++){
            	GSProviderBean b = (GSProviderBean)tList.get(i);
            	map.put(b.getGs_provider_name(),b.getGs_provider_id());
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            
        }
        return map;
    	
    }
    public boolean modifyProvider(Connection conn,String[] ids) throws GSException {
    	String tSql = "update gs_provider set gs_provider_id = ? where gs_provider_id = ?";
    	Object[] tObject = new Object[]{ids[1],ids[0]};

    	boolean tResult = false;
        try {
            tResult = dao.executeUpdate(conn,tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        System.out.println("添加:"+tResult);
        return tResult;
    }
    public boolean modify(Connection conn,GSManufactureBeanTemp aGSManufactureBean) throws GSException {

        String tSql = "UPDATE GS_Manufacture SET GS_CONCENTRICITY_ID=?, " +
                "GS_MANUFACTURE_NAME=?, GS_MANUFACTURE_CATEGORY_ID=?, GS_MANUFACTURE_BRAND=?," +
                "GS_MANUFACTURE_TYPE=?,GS_MANUFACTURE_SPEC=?,  " +
                "GS_QUANT_UNIT=?, GS_QUALITY_ASSURE_PERIOD=?,GS_MANUFACTURER_NAME=?, GS_MAKE_AREA=?," +
                "GS_PROVIDER_ID=?, GS_ADAPT_MACHINE_TYPE=?,GS_APPENDER_PROPERTY=?,GS_REMARK=?,GS_VALIDITY=?,GS_MANUFACTURE_ID=? " +
                "where GS_MANUFACTURE_ID=?";
        Object[] tObject = new Object[17];
        tObject[0] = aGSManufactureBean.getGs_manufacture_id_temp().substring(0,14);
        tObject[1] = aGSManufactureBean.getGs_manufacture_name();
        tObject[2] = aGSManufactureBean.getGs_manufacture_category_id();
        tObject[3] = aGSManufactureBean.getGs_manufacture_brand();
        tObject[4] = aGSManufactureBean.getGs_manufacture_type();
        tObject[5] = aGSManufactureBean.getGs_manufacture_spec();
        //tObject[6] = String.valueOf(aGSManufactureBean.getGs_manufacture_unitprice());
        tObject[6] = aGSManufactureBean.getGs_quant_unit();
        tObject[7] = aGSManufactureBean.getGs_quality_assure_period();
        tObject[8] = aGSManufactureBean.getGs_manufacturer_name();
        tObject[9] = aGSManufactureBean.getGs_make_area();
        tObject[10] = aGSManufactureBean.getGs_provider_id();
        tObject[11] = aGSManufactureBean.getGs_adapt_machine_type();
        tObject[12] = aGSManufactureBean.getGs_appender_property();
        tObject[13] = aGSManufactureBean.getGs_remark();
        tObject[14] = aGSManufactureBean.getGs_validity();
        tObject[15] = aGSManufactureBean.getGs_manufacture_id_temp();

        tObject[16] = aGSManufactureBean.getGs_manufacture_id();
        
        boolean tResult = false;
        try {
            tResult = dao.executeUpdate(conn,tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    public boolean modifyforsz(Connection conn,GSManufactureBeanTemp aGSManufactureBean) throws GSException {

        String tSql = "UPDATE GS_Manufacture SET GS_CONCENTRICITY_ID=?, " +
                "GS_MANUFACTURE_NAME=?, GS_MANUFACTURE_CATEGORY_ID=?, GS_MANUFACTURE_BRAND=?," +
                "GS_MANUFACTURE_TYPE=?,GS_MANUFACTURE_SPEC=?, GS_MANUFACTURE_UNITPRICE=?, " +
                "GS_QUANT_UNIT=?, GS_QUALITY_ASSURE_PERIOD=?,GS_MANUFACTURER_NAME=?, GS_MAKE_AREA=?," +
                "GS_PROVIDER_ID=?, GS_ADAPT_MACHINE_TYPE=?,GS_APPENDER_PROPERTY=?,GS_REMARK=?,GS_VALIDITY=?,GS_MANUFACTURE_ID=? " +
                "where GS_MANUFACTURE_ID=?";
        Object[] tObject = new Object[18];
        tObject[0] = aGSManufactureBean.getGs_manufacture_id_temp().substring(0,14);
        tObject[1] = aGSManufactureBean.getGs_manufacture_name();
        tObject[2] = aGSManufactureBean.getGs_manufacture_category_id();
        tObject[3] = aGSManufactureBean.getGs_manufacture_brand();
        tObject[4] = aGSManufactureBean.getGs_manufacture_type();
        tObject[5] = aGSManufactureBean.getGs_manufacture_spec();
        tObject[6] = String.valueOf(aGSManufactureBean.getGs_manufacture_unitprice());
        tObject[7] = aGSManufactureBean.getGs_quant_unit();
        tObject[8] = aGSManufactureBean.getGs_quality_assure_period();
        tObject[9] = aGSManufactureBean.getGs_manufacturer_name();
        tObject[10] = aGSManufactureBean.getGs_make_area();
        tObject[11] = aGSManufactureBean.getGs_provider_id();
        tObject[12] = aGSManufactureBean.getGs_adapt_machine_type();
        tObject[13] = aGSManufactureBean.getGs_appender_property();
        tObject[14] = aGSManufactureBean.getGs_remark();
        tObject[15] = aGSManufactureBean.getGs_validity();
        tObject[16] = aGSManufactureBean.getGs_manufacture_id_temp();

        tObject[17] = aGSManufactureBean.getGs_manufacture_id();
        
       

        boolean tResult = false;
        try {
            tResult = dao.executeUpdate(conn,tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        System.out.println("添加:"+tResult);
        return tResult;
    }
    
    public boolean add(Connection conn,GSManufactureBeanTemp aGSManufactureBean) throws GSException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String buf = df.format(new java.util.Date(System.currentTimeMillis()));
        
        String tSql="INSERT into GS_Manufacture (GS_MANUFACTURE_ID, GS_CONCENTRICITY_ID, " +
                "GS_MANUFACTURE_NAME, GS_MANUFACTURE_CATEGORY_ID, GS_MANUFACTURE_BRAND, GS_MANUFACTURE_TYPE, " +
                "GS_MANUFACTURE_SPEC, GS_MANUFACTURE_UNITPRICE, GS_QUANT_UNIT, GS_QUALITY_ASSURE_PERIOD, " +
                "GS_MANUFACTURER_NAME, GS_MAKE_AREA, GS_PROVIDER_ID, GS_ADAPT_MACHINE_TYPE, GS_MANUFACTURE_IMAGE, " +
                "GS_RECENT_ADJUSTPRICE_DATE, GS_RECENT_ADJUSTED_PRICE, GS_RECORDER_ID, " +
                "GS_MANUFACTURE_RECORD_DATE, GS_IS_TRADEFLAT_MANUFACTURE,GS_APPENDER_PROPERTY,GS_REMARK,GS_VALIDITY,gs_balance_mode) " +
                "VALUES  ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";

        Object[] tObject = new Object[24] ;
          tObject[0]=aGSManufactureBean.getGs_manufacture_id_temp();
          tObject[1]=aGSManufactureBean.getGs_manufacture_id_temp().substring(0,14);
          tObject[2]=aGSManufactureBean.getGs_manufacture_name();
          tObject[3]=new Integer(aGSManufactureBean.getGs_manufacture_category_id());
          tObject[4]=aGSManufactureBean.getGs_manufacture_brand();
          tObject[5]=aGSManufactureBean.getGs_manufacture_type();
          tObject[6]=aGSManufactureBean.getGs_manufacture_spec();
          tObject[7]=new Double(aGSManufactureBean.getGs_manufacture_unitprice());
          tObject[8]=aGSManufactureBean.getGs_quant_unit();
          tObject[9]=aGSManufactureBean.getGs_quality_assure_period();
          tObject[10]=aGSManufactureBean.getGs_manufacturer_name();
          tObject[11]=aGSManufactureBean.getGs_make_area();
          tObject[12]=aGSManufactureBean.getGs_provider_id();
          tObject[13]=aGSManufactureBean.getGs_adapt_machine_type();
          tObject[14]=aGSManufactureBean.getGs_manufacture_images();
          tObject[15]=aGSManufactureBean.getGs_recent_adjustprice_date();
          tObject[16]=new Double(aGSManufactureBean.getGs_recent_adjusted_price());
          tObject[17]=aGSManufactureBean.getGs_recorder_id();
          tObject[18]=buf;
          tObject[19]=aGSManufactureBean.getGs_is_tradeflat_manufacture();
          tObject[20]=aGSManufactureBean.getGs_appender_property();
          tObject[21]=aGSManufactureBean.getGs_remark();
          tObject[22]=aGSManufactureBean.getGs_validity();
          tObject[23]=DefineDic.getBmode(aGSManufactureBean.getGs_balance_mode());
        boolean tResult = false;
        try {
            tResult = dao.executeUpdate(conn,tSql, tObject);

        } catch (Exception ex) {
        	System.out.println(aGSManufactureBean.getGs_manufacture_id());
            GSLogWriter.error(this, ex.toString());
            throw new GSException("");
        }

        return tResult;
    }
}

//----------------------------------------------------------------------------------------------------------------

class GSManufactureBeanTemp {
    /**
     * 产品编号
     */
    private String gs_manufacture_id;
    /**
     * 临时产品编号
     */
    private String gs_manufacture_id_temp;
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 集中编码
     */
    private String gs_concentricity;
    /**
     * 供应商自用编码
     */
    //private String gs_provider_use_id;

    /**
     * 产品名称
     */
    private String gs_manufacture_name;

    /**
     * 产品种类编号
     */
    private String gs_manufacture_category_id;

    /**
     * 产品品牌
     */
    private String gs_manufacture_brand;

    /**
     * 产品型号
     */
    private String gs_manufacture_type;

    /**
     * 产品规格
     */
    private String gs_manufacture_spec;

    /**
     * 产品单价
     */
    private double gs_manufacture_unitprice;

    /**
     * 数量单位
     */
    private String gs_quant_unit;

    /**
     * 质保期
     */
    private String gs_quality_assure_period;

    /**
     * 生产厂商名称
     */
    private String gs_manufacturer_name;

    /**
     * 产地
     */
    private String gs_make_area;

    /**
     * 供应商编号
     */
    private String gs_provider_id;
    private String gs_provider_name;

    /**
     * 适用机型
     */
    private String gs_adapt_machine_type;

    /**
     * 产品图片
     */
    private String gs_manufacture_images;

    /**
     * 最近调价时间
     */
    private String gs_recent_adjustprice_date;

    /**
     * 最近调价价格
     */
    private double gs_recent_adjusted_price;

    /**
     * 录入人员帐号
     */
    private String gs_recorder_id;

    /**
     * 产品录入时间
     */
    private String gs_manufacture_record_date;

    /**
     * 是否交易平台的产品
     */
    private String gs_is_tradeflat_manufacture;
    
    private String providerName;

    /**
     * 产品附加属性
     */
    private String gs_appender_property;

    /**
     * 备注
     */
    private String gs_remark;
    
    private String gs_validity;
    
    private String gs_balance_mode="0";
    /**
     * @return the gs_balance_mode
     */
    public String getGs_balance_mode() {
        return this.gs_balance_mode;
    }

    /**
     * @param aGs_balance_mode the gs_balance_mode to set
     */
    public void setGs_balance_mode(String aGs_balance_mode) {
        this.gs_balance_mode = aGs_balance_mode;
    }

    public String getGs_validity() {
		return gs_validity;
	}

	public void setGs_validity(String gs_validity) {
		this.gs_validity = gs_validity;
	}

	/**
     * 返回gs_adapt_machine_type.
     */
    public String getGs_adapt_machine_type() {
        return this.gs_adapt_machine_type;
    }

    /**
     * @param aGs_adapt_machine_type
     *            赋值给gs_adapt_machine_type.
     */
    public void setGs_adapt_machine_type(String aGs_adapt_machine_type) {
        this.gs_adapt_machine_type = aGs_adapt_machine_type;
    }

    /**
     * 返回gs_is_tradeflat_manufacture.
     */
    public String getGs_is_tradeflat_manufacture() {
        return this.gs_is_tradeflat_manufacture;
    }

    /**
     * @param aGs_is_tradeflat_manufacture
     *            赋值给gs_is_tradeflat_manufacture.
     */
    public void setGs_is_tradeflat_manufacture(
            String aGs_is_tradeflat_manufacture) {
        this.gs_is_tradeflat_manufacture = aGs_is_tradeflat_manufacture;
    }

    /**
     * 返回gs_make_area.
     */
    public String getGs_make_area() {
        return this.gs_make_area;
    }

    /**
     * @param aGs_make_area
     *            赋值给gs_make_area.
     */
    public void setGs_make_area(String aGs_make_area) {
        this.gs_make_area = aGs_make_area;
    }

    /**
     * 返回gs_manufacture_brand.
     */
    public String getGs_manufacture_brand() {
        return this.gs_manufacture_brand;
    }

    /**
     * @param aGs_manufacture_brand
     *            赋值给gs_manufacture_brand.
     */
    public void setGs_manufacture_brand(String aGs_manufacture_brand) {
        this.gs_manufacture_brand = aGs_manufacture_brand;
    }

    /**
     * 返回gs_manufacture_images.
     */
    public String getGs_manufacture_images() {
        return this.gs_manufacture_images;
    }

    /**
     * @param aGs_manufacture_images
     *            赋值给gs_manufacture_images.
     */
    public void setGs_manufacture_images(String aGs_manufacture_images) {
        this.gs_manufacture_images = aGs_manufacture_images;
    }

    /**
     * 返回gs_manufacture_name.
     */
    public String getGs_manufacture_name() {
        return this.gs_manufacture_name;
    }

    /**
     * @param aGs_manufacture_name
     *            赋值给gs_manufacture_name.
     */
    public void setGs_manufacture_name(String aGs_manufacture_name) {
        this.gs_manufacture_name = aGs_manufacture_name;
    }

    /**
     * 返回gs_manufacture_record_date.
     */
    public String getGs_manufacture_record_date() {
        return this.gs_manufacture_record_date;
    }

    /**
     * @param aGs_manufacture_record_date
     *            赋值给gs_manufacture_record_date.
     */
    public void setGs_manufacture_record_date(String aGs_manufacture_record_date) {
        this.gs_manufacture_record_date = aGs_manufacture_record_date;
    }

    /**
     * 返回gs_manufacture_spec.
     */
    public String getGs_manufacture_spec() {
        return this.gs_manufacture_spec;
    }

    /**
     * @param aGs_manufacture_spec
     *            赋值给gs_manufacture_spec.
     */
    public void setGs_manufacture_spec(String aGs_manufacture_spec) {
        this.gs_manufacture_spec = aGs_manufacture_spec;
    }

    /**
     * 返回gs_manufacture_type.
     */
    public String getGs_manufacture_type() {
        return this.gs_manufacture_type;
    }

    /**
     * @param aGs_manufacture_type
     *            赋值给gs_manufacture_type.
     */
    public void setGs_manufacture_type(String aGs_manufacture_type) {
        this.gs_manufacture_type = aGs_manufacture_type;
    }

    /**
     * 返回gs_manufacture_unitprice.
     */
    public double getGs_manufacture_unitprice() {
        return this.gs_manufacture_unitprice;
    }

    /**
     * @param aGs_manufacture_unitprice
     *            赋值给gs_manufacture_unitprice.
     */
    public void setGs_manufacture_unitprice(double aGs_manufacture_unitprice) {
        this.gs_manufacture_unitprice = aGs_manufacture_unitprice;
    }

    /**
     * 返回gs_manufacturer_name.
     */
    public String getGs_manufacturer_name() {
        return this.gs_manufacturer_name;
    }

    /**
     * @param aGs_manufacturer_name
     *            赋值给gs_manufacturer_name.
     */
    public void setGs_manufacturer_name(String aGs_manufacturer_name) {
        this.gs_manufacturer_name = aGs_manufacturer_name;
    }

    /**
     * 返回gs_provider_id.
     */
    public String getGs_provider_id() {
        return this.gs_provider_id;
    }

    /**
     * @param aGs_provider_id
     *            赋值给gs_provider_id.
     */
    public void setGs_provider_id(String aGs_provider_id) {
        this.gs_provider_id = aGs_provider_id;
    }

   

    /**
     * 返回gs_quality_assure_period.
     */
    public String getGs_quality_assure_period() {
        return this.gs_quality_assure_period;
    }

    /**
     * @param aGs_quality_assure_period
     *            赋值给gs_quality_assure_period.
     */
    public void setGs_quality_assure_period(String aGs_quality_assure_period) {
        this.gs_quality_assure_period = aGs_quality_assure_period;
    }

    /**
     * 返回gs_quant_unit.
     */
    public String getGs_quant_unit() {
        return this.gs_quant_unit;
    }

    /**
     * @param aGs_quant_unit
     *            赋值给gs_quant_unit.
     */
    public void setGs_quant_unit(String aGs_quant_unit) {
        this.gs_quant_unit = aGs_quant_unit;
    }

    /**
     * 返回gs_recent_adjusted_price.
     */
    public double getGs_recent_adjusted_price() {
        return this.gs_recent_adjusted_price;
    }

    /**
     * @param aGs_recent_adjusted_price
     *            赋值给gs_recent_adjusted_price.
     */
    public void setGs_recent_adjusted_price(double aGs_recent_adjusted_price) {
        this.gs_recent_adjusted_price = aGs_recent_adjusted_price;
    }

    /**
     * 返回gs_recent_adjustprice_date.
     */
    public String getGs_recent_adjustprice_date() {
        return this.gs_recent_adjustprice_date;
    }

    /**
     * @param aGs_recent_adjustprice_date
     *            赋值给gs_recent_adjustprice_date.
     */
    public void setGs_recent_adjustprice_date(String aGs_recent_adjustprice_date) {
        this.gs_recent_adjustprice_date = aGs_recent_adjustprice_date;
    }

    /**
     * 返回gs_recorder_id.
     */
    public String getGs_recorder_id() {
        return this.gs_recorder_id;
    }

    /**
     * @param aGs_recorder_id
     *            赋值给gs_recorder_id.
     */
    public void setGs_recorder_id(String aGs_recorder_id) {
        this.gs_recorder_id = aGs_recorder_id;
    }

    public GSManufactureBean getGSManufacutreBean(GSManufactureBeanTemp temp){
    	GSManufactureBean bean = new GSManufactureBean();
    	try{
    		BeanUtils.copyProperties(temp,bean);
    	}catch(Exception e ){
    		e.printStackTrace();
    	}
    	return bean;
    }
    public Object process(ResultSet aResultSet) throws SQLException {
      GSManufactureBeanTemp tGSManufactureBean = new GSManufactureBeanTemp();
        tGSManufactureBean.gs_adapt_machine_type=aResultSet.getString("GS_ADAPT_MACHINE_TYPE")== null ? "" :aResultSet.getString("GS_ADAPT_MACHINE_TYPE").trim();
        tGSManufactureBean.gs_is_tradeflat_manufacture=aResultSet.getString("GS_IS_TRADEFLAT_MANUFACTURE").trim();
        tGSManufactureBean.gs_make_area=aResultSet.getString("GS_MAKE_AREA")== null ? "" :aResultSet.getString("GS_MAKE_AREA").trim();
        tGSManufactureBean.gs_manufacture_brand=aResultSet.getString("GS_MANUFACTURE_BRAND") == null? "":aResultSet.getString("GS_MANUFACTURE_BRAND").trim();
        
        tGSManufactureBean.gs_manufacture_category_id=aResultSet.getString("GS_MANUFACTURE_CATEGORY_ID")== null ? "" :aResultSet.getString("GS_MANUFACTURE_CATEGORY_ID").trim();
        
        tGSManufactureBean.gs_concentricity=aResultSet.getString("GS_CONCENTRICITY_ID")==null?"":aResultSet.getString("GS_CONCENTRICITY_ID").trim();
        
        tGSManufactureBean.gs_manufacture_images=aResultSet.getString("GS_MANUFACTURE_IMAGE")== null ? "" :aResultSet.getString("GS_MANUFACTURE_IMAGE").trim();
        
        tGSManufactureBean.gs_manufacture_name=aResultSet.getString("GS_MANUFACTURE_NAME").trim();
        
        tGSManufactureBean.gs_manufacture_record_date=aResultSet.getString("GS_MANUFACTURE_RECORD_DATE")==null? "":aResultSet.getString("GS_MANUFACTURE_RECORD_DATE").trim();
        tGSManufactureBean.gs_manufacture_spec=aResultSet.getString("GS_MANUFACTURE_SPEC")== null ? "" :aResultSet.getString("GS_MANUFACTURE_SPEC").trim();
        tGSManufactureBean.gs_manufacture_type=aResultSet.getString("GS_MANUFACTURE_TYPE")== null ? "" :aResultSet.getString("GS_MANUFACTURE_TYPE").trim();
        
        tGSManufactureBean.gs_manufacture_unitprice=aResultSet.getDouble("GS_MANUFACTURE_UNITPRICE");
        
        tGSManufactureBean.gs_manufacturer_name=aResultSet.getString("GS_MANUFACTURER_NAME")== null ? "" :aResultSet.getString("GS_MANUFACTURER_NAME").trim();
        
        tGSManufactureBean.gs_provider_id=aResultSet.getString("GS_PROVIDER_ID");
       
        tGSManufactureBean.gs_manufacture_id=aResultSet.getString("GS_MANUFACTURE_ID")== null ? "" :aResultSet.getString("GS_MANUFACTURE_ID").trim();
        tGSManufactureBean.gs_manufacture_id=aResultSet.getString("GS_MANUFACTURE_ID_TEMP")== null ? "" :aResultSet.getString("GS_MANUFACTURE_ID_TEMP").trim();
        tGSManufactureBean.gs_quality_assure_period=aResultSet.getString("GS_QUALITY_ASSURE_PERIOD") == null? "":aResultSet.getString("GS_QUALITY_ASSURE_PERIOD").trim();
        tGSManufactureBean.gs_quant_unit=aResultSet.getString("GS_QUANT_UNIT");
        tGSManufactureBean.gs_recent_adjusted_price=aResultSet.getDouble("GS_RECENT_ADJUSTED_PRICE") ;
        tGSManufactureBean.gs_recent_adjustprice_date=aResultSet.getString("GS_RECENT_ADJUSTPRICE_DATE")== null ? "" :aResultSet.getString("GS_RECENT_ADJUSTPRICE_DATE").trim();
        tGSManufactureBean.gs_recorder_id=aResultSet.getString("GS_RECORDER_ID")== null ? "" :aResultSet.getString("GS_RECORDER_ID").trim();
        tGSManufactureBean.gs_appender_property = aResultSet.getString("GS_APPENDER_PROPERTY") == null?"":aResultSet.getString("GS_APPENDER_PROPERTY").trim();
        tGSManufactureBean.gs_remark = aResultSet.getString("GS_REMARK") == null? "":aResultSet.getString("GS_REMARK").trim();
        //added by simonyi 2006-9-29
        tGSManufactureBean.gs_validity = aResultSet.getString("GS_VALIDITY") == null? "1":aResultSet.getString("GS_VALIDITY").trim();
        //added by simonyi 2006-3-14
        try{
            int index = aResultSet.findColumn("PROVIDERNAME");
            tGSManufactureBean.providerName = aResultSet.getString(index);

            
        }catch(SQLException e){
            
        }
        
      return tGSManufactureBean;
    }

    /**
     * @return 返回 gs_concentricity.
     */
    public String getGs_concentricity() {
        return this.gs_concentricity;
    }

    /**
     * @param aGs_concentricity The gs_concentricity to set.
     */
    public void setGs_concentricity(String aGs_concentricity) {
        this.gs_concentricity = aGs_concentricity;
    }

    /**
     * @return 返回 gs_manufacture_id.
     */
    public String getGs_manufacture_id_temp() {
        return this.gs_manufacture_id_temp;
    }

    /**
     * @param aGs_manufacture_id The gs_manufacture_id to set.
     */
    public void setGs_manufacture_id_temp(String aGs_manufacture_id_temp) {
        this.gs_manufacture_id_temp = aGs_manufacture_id_temp;
    }
    
    /**
     * @return 返回 gs_manufacture_id.
     */
    public String getGs_manufacture_id() {
        return this.gs_manufacture_id;
    }

    /**
     * @param aGs_manufacture_id The gs_manufacture_id to set.
     */
    public void setGs_manufacture_id(String aGs_manufacture_id) {
        this.gs_manufacture_id = aGs_manufacture_id;
    }

    /**
     * @return 返回 gs_manufacture_category_id.
     */
    public String getGs_manufacture_category_id() {
        return this.gs_manufacture_category_id;
    }

    /**
     * @param aGs_manufacture_category_id The gs_manufacture_category_id to set.
     */
    public void setGs_manufacture_category_id(String aGs_manufacture_category_id) {
        this.gs_manufacture_category_id = aGs_manufacture_category_id;
    }

    public String getGs_provider_name() {
        return gs_provider_name;
    }
    public void setGs_provider_name(String gs_provider_name) {
        this.gs_provider_name = gs_provider_name;
    }

    /**
     * @return 返回 gs_appender_property.
     */
    public String getGs_appender_property() {
        return this.gs_appender_property;
    }

    /**
     * @param aGs_appender_property The gs_appender_property to set.
     */
    public void setGs_appender_property(String aGs_appender_property) {
        this.gs_appender_property = aGs_appender_property;
    }

    /**
     * @return 返回 gs_remark.
     */
    public String getGs_remark() {
        return this.gs_remark;
    }

    /**
     * @param aGs_remark The gs_remark to set.
     */
    public void setGs_remark(String aGs_remark) {
        this.gs_remark = aGs_remark;
    }
    
    public String getProviderName() {
        return providerName;
    }
    public void setProviderName(String providerName) {
        this.providerName = providerName;
    }
}
